package com.leyou.cart.listenner;

import com.leyou.cart.Service.CartService;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class CartListenner {

    @Autowired
    private CartService cartService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "leyou.cart.queue",durable = "true"),
            exchange = @Exchange(value = "leyou.cart.exchange",ignoreDeclarationExceptions = "true"),
            key = {"cart.delete.cart"}))
    public void listenCart(Map<String,String> cartMap){
        String s = cartMap.get("uid");
        String skuIds = cartMap.get("skuIds");
        String[] split = skuIds.split(" ");
        for(int a = 0 ; a < split.length ; a++ ){
            this.cartService.deleteCart(s,Long.parseLong(split[a]));
        }
    }
}
